package com.liuceng.peatio.utils

import com.liuceng.peatio.model.NewRptMemberMediumMobileDailyFlow
import com.liuceng.peatio.model.NewRptMemberMediumPcDailyFlow
import spock.lang.Specification

/**
 * Created by liyuhang on 2016-28-4.
 */
class AppUtilsSpec extends Specification {

    def "测试获取需要计算的移动端数据"() {
        given:

        //各个渠道已经处理的数据
        NewRptMemberMediumMobileDailyFlow dealedData1 = new NewRptMemberMediumMobileDailyFlow(memberId: 1, cost: new BigDecimal(10), getphoneNum: 0, mediumId: 1)
        NewRptMemberMediumMobileDailyFlow dealedData2 = new NewRptMemberMediumMobileDailyFlow(memberId: 2, cost: new BigDecimal(100), getphoneNum: 2, mediumId: 2)
        NewRptMemberMediumMobileDailyFlow dealedData3 = new NewRptMemberMediumMobileDailyFlow(memberId: 3, cost: new BigDecimal(200), getphoneNum: 10, mediumId: 3)
        //将已经处理的数据添加到用户下的渠道map中
        Map<Integer, NewRptMemberMediumMobileDailyFlow> dealedMediumDataMap1 = new HashMap<>();
        dealedMediumDataMap1.put(1, dealedData1)
        Map<Integer, NewRptMemberMediumMobileDailyFlow> dealedMediumDataMap2 = new HashMap<>();
        dealedMediumDataMap2.put(2, dealedData2)
        Map<Integer, NewRptMemberMediumMobileDailyFlow> dealedMediumDataMap3 = new HashMap<>();
        dealedMediumDataMap3.put(3, dealedData3)
        //将渠道map数据按key：：memberId 放入账户map中
        Map<Integer, Map<Integer, NewRptMemberMediumMobileDailyFlow>> dealedDataMap2 = new HashMap<>();
        dealedDataMap2.put(1, dealedMediumDataMap1);
        dealedDataMap2.put(2, dealedMediumDataMap2);
        dealedDataMap2.put(3, dealedMediumDataMap3);

        //各个账户下各个渠道没有计算额的数据
        NewRptMemberMediumMobileDailyFlow notDealedData1 = new NewRptMemberMediumMobileDailyFlow(memberId: 1, cost: new BigDecimal(10), getphoneNum: 0, mediumId: 1)
        NewRptMemberMediumMobileDailyFlow notDealedData2 = new NewRptMemberMediumMobileDailyFlow(memberId: 3, cost: new BigDecimal(300), getphoneNum: 13, mediumId: 3)
        NewRptMemberMediumMobileDailyFlow notDealedData3 = new NewRptMemberMediumMobileDailyFlow(memberId: 4, cost: new BigDecimal(200), getphoneNum: 10, mediumId: 4)
        //将已经处理的数据添加到用户下的渠道map中
        List<NewRptMemberMediumMobileDailyFlow> notDealedMediumDataList1 = [notDealedData1]
        List<NewRptMemberMediumMobileDailyFlow> notDealedMediumDataList2 = [notDealedData2]
        List<NewRptMemberMediumMobileDailyFlow> notDealedMediumDataList3 = [notDealedData3]
        Map<Integer, Map<Integer, NewRptMemberMediumMobileDailyFlow>> notDealedDataMap2 = new HashMap<>();
        notDealedDataMap2.put(1, notDealedMediumDataList1);
        notDealedDataMap2.put(3, notDealedMediumDataList2);
        notDealedDataMap2.put(4, notDealedMediumDataList3);

        //账户
        Map<String, Integer> memberId1 = ['member_id': 1]
        Map<String, Integer> memberId2 = ['member_id': 2]
        Map<String, Integer> memberId3 = ['member_id': 3]
        Map<String, Integer> memberId4 = ['member_id': 4]
        List<Map<String, Integer>> memberIds = [memberId1, memberId2, memberId3, memberId4]



        when:
        Map<Integer, List<NewRptMemberMediumMobileDailyFlow>> result = AppUtils.getNeedCalculateMobileDatas(dealedDataMap2, notDealedDataMap2, memberIds)

        then: "应该返回两条记录"
        result.size() == 2

        and:
        result.get(3)[0].getGetphoneNum() == 3 && result.get(3)[0].getCost().compareTo(new BigDecimal(100)) == 0

        and:
        result.get(4)[0].getGetphoneNum() == 10 && result.get(4)[0].getCost().compareTo(new BigDecimal(200)) == 0
    }

    def "测试获取需要计算的PC端数据"() {
        given:
        //各个渠道已经处理的数据
        NewRptMemberMediumPcDailyFlow dealedData1 = new NewRptMemberMediumPcDailyFlow(memberId: 1, cost: new BigDecimal(10), validNum: 0, mediumId: 1)
        NewRptMemberMediumPcDailyFlow dealedData2 = new NewRptMemberMediumPcDailyFlow(memberId: 2, cost: new BigDecimal(100), validNum: 2, mediumId: 2)
        NewRptMemberMediumPcDailyFlow dealedData3 = new NewRptMemberMediumPcDailyFlow(memberId: 3, cost: new BigDecimal(200), validNum: 10, mediumId: 3)
        //将已经处理的数据添加到用户下的渠道map中
        Map<Integer, NewRptMemberMediumPcDailyFlow> dealedMediumDataMap1 = new HashMap<>();
        dealedMediumDataMap1.put(1, dealedData1)
        Map<Integer, NewRptMemberMediumMobileDailyFlow> dealedMediumDataMap2 = new HashMap<>();
        dealedMediumDataMap2.put(2, dealedData2)
        Map<Integer, NewRptMemberMediumMobileDailyFlow> dealedMediumDataMap3 = new HashMap<>();
        dealedMediumDataMap3.put(3, dealedData3)
        //将渠道map数据按key：：memberId 放入账户map中
        Map<Integer, Map<Integer, NewRptMemberMediumPcDailyFlow>> dealedDataMap2 = new HashMap<>();
        dealedDataMap2.put(1, dealedMediumDataMap1);
        dealedDataMap2.put(2, dealedMediumDataMap2);
        dealedDataMap2.put(3, dealedMediumDataMap3);

        //各个账户下各个渠道没有计算额的数据
        NewRptMemberMediumPcDailyFlow notDealedData1 = new NewRptMemberMediumPcDailyFlow(memberId: 1, cost: new BigDecimal(10), validNum: 0, mediumId: 1)
        NewRptMemberMediumPcDailyFlow notDealedData2 = new NewRptMemberMediumPcDailyFlow(memberId: 3, cost: new BigDecimal(300), validNum: 13, mediumId: 3)
        NewRptMemberMediumPcDailyFlow notDealedData3 = new NewRptMemberMediumPcDailyFlow(memberId: 4, cost: new BigDecimal(200), validNum: 10, mediumId: 4)
        //将已经处理的数据添加到用户下的渠道map中
        List<NewRptMemberMediumMobileDailyFlow> notDealedMediumDataList1 = [notDealedData1]
        List<NewRptMemberMediumMobileDailyFlow> notDealedMediumDataList2 = [notDealedData2]
        List<NewRptMemberMediumMobileDailyFlow> notDealedMediumDataList3 = [notDealedData3]
        Map<Integer, Map<Integer, NewRptMemberMediumMobileDailyFlow>> notDealedDataMap2 = new HashMap<>();
        notDealedDataMap2.put(1, notDealedMediumDataList1);
        notDealedDataMap2.put(3, notDealedMediumDataList2);
        notDealedDataMap2.put(4, notDealedMediumDataList3);

        //账户
        Map<String, Integer> memberId1 = ['member_id': 1]
        Map<String, Integer> memberId2 = ['member_id': 2]
        Map<String, Integer> memberId3 = ['member_id': 3]
        Map<String, Integer> memberId4 = ['member_id': 4]
        List<Map<String, Integer>> memberIds = [memberId1, memberId2, memberId3, memberId4]

        when:
        Map<Integer, List<NewRptMemberMediumPcDailyFlow>> result = AppUtils.getNeedCalculatePcDatas(dealedDataMap2, notDealedDataMap2, memberIds)

        then: "应该返回两条记录"
        result.size() == 2

        and:
        result.get(3)[0].getValidNum() == 3 && result.get(3)[0].getCost().compareTo(new BigDecimal(100)) == 0

        and:
        result.get(4)[0].getValidNum() == 10 && result.get(4)[0].getCost().compareTo(new BigDecimal(200)) == 0
    }
}